﻿using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

public class AI : MonoBehaviour
{
    public static HashSet<AI> all = new HashSet<AI>();

    private void Awake()
    {
        this.InitCaches();
        all.Add(this);
    }

    private void InitCaches()
    {
        this.self = this.GetRoot();
        this.character = this.self.GetComponent<Character>();
        this.inventory = this.self.GetComponent<Inventory>();
        this.life = this.self.GetComponent<Life>();
        this.attack = this.self.GetComponent<Attack>();
        this.look = this.self.GetComponent<Look>();
        this.move = this.self.GetComponent<Move>();
        this.targetDetector = this.self.GetComponent<AITargetDetector>();
    }

    private void OnDestroy()
    {
        all.Remove(this);
    }

    private void Start()
    {
        this.InitCaches();
    }

    public Attack attack { get; private set; }

    public bool canWeaponHitTarget
    {
        get
        {
            return ((this.weapon != null) && (Vector3.Distance(this.position, this.target.transform.position) < (this.weapon.attackRange + 0.5f)));
        }
    }

    public Character character { get; private set; }

    public Inventory inventory { get; private set; }

    public Life life { get; private set; }

    public Look look { get; private set; }

    public Move move { get; private set; }

    public Vector3 position
    {
        get
        {
            return this.self.transform.position;
        }
        set
        {
            this.self.transform.position = value;
        }
    }

    public GameObject self { get; private set; }

    public GameObject target
    {
        get
        {
            return ((this.targetDetector == null) ? null : this.targetDetector.target);
        }
    }

    public AITargetDetector targetDetector { get; private set; }

    public Weapon weapon
    {
        get
        {
            return ((this.attack == null) ? null : this.attack.weapon);
        }
        set
        {
            this.attack.weapon = value;
        }
    }
}

